package Gui;

import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;

import Logic.LoginInfo;
import client.LibaryMain;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JTextField;

import org.jfree.ui.RefineryUtilities;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JScrollBar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
 * this class initilaize the activity report window
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class ActivityReport extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private String[] str; 
	private String[] str1;
	private String   str11;
	private String   str12;
	private String   str13;
	private String[] str2;
	private String[] str3;
	private JTable table;
	private JTable table_1;

/**
 * constractor of acrivity report 
 * @param str
 */
	public ActivityReport(String[] str) {
	
		initcomponents(str);
		MainScreen.card.add("reportactivity", this);

		MainScreen.cardLayot.show(MainScreen.card, "reportactivity");
		
	}
/**
 * this method init the activity report window 
 * @param str
 */
	public void initcomponents(String[] str)
	{
	setBackground(new Color(153, 204, 255));
	setSize(1000,710);
	setLayout(null);

	int i=0;
	ArrayList<Object> arr1 = new ArrayList<Object>();

	for ( i= 0 ;i< str.length ; i++)
	{	
	if (!(str[i].equals("activityselected")))
			arr1.add(str[i+1]);
	else break;

	}
	
	str11= arr1.toString().substring(1, arr1.toString().length()-1);
	str1 = str11.toString().split("[ , ] ");
	
	int k=i;
	i=0;
	ArrayList<Object> arr2 = new ArrayList<Object>();
	for ( ; k < str.length ; k++,i++)
	{	if (!(str[k].equals("activitydate")))
			arr2.add(str[k+1]);
		else break;
	}
	  str12= arr2.toString().substring(1, arr2.toString().length()-1);
	  str2 = str12.toString().split("[ , ] ");
	  
	int j=k;
	i=0;
	ArrayList<Object> arr3 = new ArrayList<Object>();
	{for ( ; j < str.length ; j++,i++)
		arr3.add(str[j]);

	}
	str13= arr3.toString().substring(1, arr3.toString().length()-1);
	str3 = str13.toString().split("[ , ] ");
	
	
	Vector<Vector<Object>> data = new Vector<Vector<Object>>();
	
	 for (int z=0; z<=(str1.length-7);z=z+7){
	        Vector<Object> row = creatrow(str1,z);
	        data.add(row);
	        }
	
	 Vector<Vector<Object>> data2 = new Vector<Vector<Object>>();
		
	 for (int z=0; z<=(str2.length-7);z=z+7){
	        Vector<Object> row1 = creatrow(str2,z);
	        data2.add(row1);
	        }
	
	 Vector<String> title = new Vector<String>();
		title.add("report");
		title.add("Active Users");
		title.add("Locked Users");
		title.add("Freeze Users");
	    title.add("Lend Books");
	    title.add("Books that didn't returnd in time");
	    title.add("Report Date");
	
	    
	JScrollPane scrollPane = new JScrollPane();
	scrollPane.setBounds(99, 163, 762, 378);
	add(scrollPane);
	
	JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
	scrollPane.setViewportView(tabbedPane);
	
	JScrollPane scrollPane_1 = new JScrollPane();
	tabbedPane.addTab("Daily Report", null, scrollPane_1, null);
	
	table_1 = new JTable(data,title);
	scrollPane_1.setViewportView(table_1);
	
	table_1.addMouseListener(new MouseAdapter()    
    {   
    public void mouseClicked(MouseEvent evt)   
    {   
  //  int col = table.getSelectedColumn();   
    int row= table_1.getSelectedRow();
   
  
    int active=Integer.parseInt((String) table_1.getValueAt(row,1));
    int lock= Integer.parseInt((String) table_1.getValueAt(row,2));
    int freeze=Integer.parseInt((String) table_1.getValueAt(row,3));
    int lend=Integer.parseInt((String) table_1.getValueAt(row,4));
    int late=Integer.parseInt((String) table_1.getValueAt(row,5));
    
    final Histogram histo =new Histogram("Activity Diagram",active,lock,freeze,lend,late);
	RefineryUtilities.centerFrameOnScreen(histo);
	histo.setVisible(true);
	histo.pack();
    RefineryUtilities.centerFrameOnScreen(histo);
    histo.setVisible(true);
    histo.toFront();

    }   
    
    }); 
	
	
	
	
	
	JScrollPane History = new JScrollPane();
	
	
	
	tabbedPane.addTab("History", null, History, null);
	
	table = new JTable(data2,title);
	History.setViewportView(table);
	
	
	table.addMouseListener(new MouseAdapter()    
    {   
    public void mouseClicked(MouseEvent evt)   
    {   
  //  int col = table.getSelectedColumn();   
    int row= table.getSelectedRow();
   
  
    int active=Integer.parseInt((String) table.getValueAt(row,1));
    int lock= Integer.parseInt((String) table.getValueAt(row,2));
    int freeze=Integer.parseInt((String) table.getValueAt(row,3));
    int lend=Integer.parseInt((String) table.getValueAt(row,4));
    int late=Integer.parseInt((String) table.getValueAt(row,5));
    
    final Histogram histo =new Histogram("Activity Diagram",active,lock,freeze,lend,late);
	RefineryUtilities.centerFrameOnScreen(histo);
	histo.setVisible(true);
	histo.pack();
    RefineryUtilities.centerFrameOnScreen(histo);
    histo.setVisible(true);
    histo.toFront();

    }   
    
    }); 
	
	
	final JComboBox<String>  comboBox = new JComboBox();
	for ( int w = 1 ; w <= this.str3.length-1; w ++)
		comboBox.addItem(this.str3[w]);
	
	comboBox.setBounds(220, 572, 155, 22);
	add(comboBox);
	
	

	final JComboBox<String>  comboBox_1 = new JComboBox();
	for ( int w = 1 ; w <= this.str3.length-1; w ++)
		comboBox_1.addItem(this.str3[w]);
	
	
	
	
	comboBox_1.setBounds(482, 572, 161, 22);
	add(comboBox_1);
	
	JLabel lblStartDate = new JLabel("Start Date");
	lblStartDate.setBounds(131, 575, 77, 16);
	add(lblStartDate);
	
	JLabel lblEndDate = new JLabel("End Date");
	lblEndDate.setBounds(414, 575, 56, 16);
	add(lblEndDate);
	
	JLabel lblActivityReport = new JLabel("Activity Report");
	lblActivityReport.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 26));
	lblActivityReport.setBounds(41, 35, 296, 37);
	add(lblActivityReport);


	

	
	JButton btnGenerateReport = new JButton("Generate Report");
	btnGenerateReport.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			
			String start = (String) comboBox.getSelectedItem(); 
			String End =   (String) comboBox_1.getSelectedItem(); 
		if ( start.compareTo(End) > 0 )
		{
			JOptionPane.showMessageDialog(null,"Please enter start date greater  then end date", "MESSAGE",JOptionPane.INFORMATION_MESSAGE);

		}
		else
			{
			ArrayList<Object> Arr=new ArrayList<Object>();
			Arr.add("selectedDates");
			Arr.add(comboBox.getSelectedItem());
			Arr.add(comboBox_1.getSelectedItem());
			LibaryMain.chat.handleMessageFromGui(Arr);
			}
			
		}
	});
	
	btnGenerateReport.setFont(new Font("Tahoma", Font.BOLD, 15));
	btnGenerateReport.setBounds(693, 570, 168, 25);
	add(btnGenerateReport);
	
	JButton btnBack = new JButton("Back");
	btnBack.addActionListener(new ActionListener() {
		
		public void actionPerformed(ActionEvent e) {
			
			MainScreen.cardLayot.show(MainScreen.card, "reportmenu");
		}
	});
	btnBack.setFont(new Font("Tahoma", Font.BOLD, 14));
	btnBack.setBounds(41, 638, 97, 25);
	add(btnBack);
	
	JButton btnLogout = new JButton("Logout");
	btnLogout.setFont(new Font("Tahoma", Font.BOLD, 14));
	btnLogout.setBounds(855, 638, 97, 25);
	add(btnLogout);
	
	JLabel label = new JLabel("");
	label.setIcon(new ImageIcon(ActivityReport.class.getResource("/Gui/images/column_chart.png")));
	label.setBounds(580, 225, 618, 400);
	add(label);
	btnLogout.addActionListener(new java.awt.event.ActionListener() {      
        public void actionPerformed(java.awt.event.ActionEvent ev) {
        	btnLogOut(ev);				
		}
        
		private void btnLogOut(ActionEvent ev) {
			ArrayList<String> Arr=new ArrayList<String>();
			Arr.add("Logout");
			Arr.add("Worker");
			Arr.add(LoginInfo.UserName);
			LibaryMain.chat.handleMessageFromGui(Arr);
			MainScreen.cardLayot.show(MainScreen.card, "Login");
			
		}
    });
	}

private Vector<Object> creatrow(String[] str,int i) {
	Vector<Object> row = new Vector<Object>();
	row.add( ""+str[i]);
    row.add( ""+str[i+1]);
    row.add( ""+str[i+2]);
    row.add( ""+str[i+3]);
    row.add( ""+str[i+4]);
    row.add( ""+str[i+5]);
    row.add( ""+str[i+6]);
	return row;
}
}